package com.barbeque.mall.controller;

import com.barbeque.mall.bean.BaseRespVo;
import com.barbeque.mall.bean.advertise.PO.TopicDeletePo;
import com.barbeque.mall.bean.advertise.BO.TopicListBo;
import com.barbeque.mall.bean.advertise.MarketTopic;
import com.barbeque.mall.bean.advertise.VO.MyBaseRespVo;
import com.barbeque.mall.bean.advertise.VO.TopicUpdateVo;
import com.barbeque.mall.bean.param.CommonData;
import com.barbeque.mall.service.TopicService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author bai
 * @since 2022/07/18 15:53
 */
@RestController
@RequestMapping("admin/topic")
public class TopicController {
    @Autowired
    TopicService topicService;

    // localhost:8083/admin/topic/list?page=1&limit=20&sort=add_time&order=desc
    @RequestMapping("list")
    @RequiresPermissions("admin:topic:list")
    public BaseRespVo list(TopicListBo topicListBo){
        CommonData<MarketTopic> marketTopicCommonData = new CommonData<>();
        if(topicListBo.getTitle() == null && topicListBo.getSubtitle() == null){
            // 查询专题列表
            marketTopicCommonData = topicService.queryTopicList(topicListBo);
        }else {
            // 模糊查询操作
            marketTopicCommonData = topicService.queryTopicListCondition(topicListBo);
        }

        return BaseRespVo.ok(marketTopicCommonData);
    }

    // localhost:8083/admin/topic/create
    @RequestMapping("create")
    @RequiresPermissions("admin:topic:create")
    public BaseRespVo create(@RequestBody MarketTopic marketTopic){

        marketTopic = topicService.insertTopicInfo(marketTopic);

        return BaseRespVo.ok(marketTopic);
    }

    // localhost:8083/admin/topic/read?id=315
    @RequestMapping("read")
    @RequiresPermissions("admin:topic:read")
    public BaseRespVo read(@RequestParam("id") Integer id){

        TopicUpdateVo topicUpdateVo = topicService.queryTopicInfoById(id);

        return BaseRespVo.ok(topicUpdateVo);
    }

    // localhost:8083/admin/topic/update
    @RequestMapping("update")
    @RequiresPermissions("admin:topic:update")
    public BaseRespVo update(@RequestBody MarketTopic marketTopic){
        marketTopic = topicService.updateTopicInfo(marketTopic);

         return BaseRespVo.ok(marketTopic);
    }

    // localhost:8083/admin/topic/delete
    @RequestMapping("delete")
    @RequiresPermissions("admin:topic:delete")
    public MyBaseRespVo delete(@RequestBody MarketTopic marketTopic){

        topicService.deleteTopicInfo(marketTopic);

        return MyBaseRespVo.ok();
    }

    // localhost:8083/admin/topic/batch-delete
    @RequestMapping("batch-delete")
    @RequiresPermissions("admin:topic:batch-delete")
    public MyBaseRespVo batchDelete(@RequestBody TopicDeletePo topicDeletePo){

        topicService.deleteTopicInfoByIds(topicDeletePo);

        return MyBaseRespVo.ok();
    }
}
